import 'package:enjoy_plus_demo/pages/home/components/detail.dart';
import 'package:enjoy_plus_demo/pages/home/index.dart';
import 'package:enjoy_plus_demo/pages/house/building_list.dart';
import 'package:enjoy_plus_demo/pages/house/house_detail.dart';
import 'package:enjoy_plus_demo/pages/house/house_form.dart';
import 'package:enjoy_plus_demo/pages/house/house_list.dart';
import 'package:enjoy_plus_demo/pages/house/location_list.dart';
import 'package:enjoy_plus_demo/pages/house/room_list.dart';
import 'package:enjoy_plus_demo/pages/login/index.dart';
import 'package:enjoy_plus_demo/pages/mine/index.dart';
import 'package:enjoy_plus_demo/pages/profile/index.dart';
import 'package:enjoy_plus_demo/pages/tab_bar/index.dart';
import 'package:enjoy_plus_demo/utils/token.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
        // home: MinePage(),
        // home: HomePage()
        routes: {
          //
          '/': (context) => const TabBarPage(),
          '/detail': (context) => const NoticeDetail(),
          '/login': (context) => const LoginPage()
        },
        //获取用户跳转页面的路径
        //如果有token 直接放行
        onGenerateRoute: (settings) {
          final tokenManager = TokenManager();
          final token = tokenManager.getToken() ?? '';
          if (token.isEmpty && settings.name != '/login') {
            //如果没有token 而且去的页面不是登录页面 跳转到登录页面
            return MaterialPageRoute(builder: (context) => const LoginPage());
          }
          //去编辑个人信息页面
          if (settings.name == '/profile') {
            return MaterialPageRoute(
                builder: (context) =>
                    ProfilePage(userInfo: settings.arguments as Map));
          }
          //去我的房屋列表
          //去编辑个人信息页面
          if (settings.name == '/house_list') {
            return MaterialPageRoute(builder: (context) => HouseList());
          }
          //去获取当前地址和附近社区页面
          if (settings.name == '/location_list') {
            return MaterialPageRoute(builder: (context) => LocationList());
          }
          //楼栋列表
          if (settings.name == '/building_list') {
            return MaterialPageRoute(
                builder: (context) => BuildingList(
                      point: (settings.arguments as Map)['point'],
                    ));
          }

          //楼栋列表
          if (settings.name == '/room_list') {
            Map temp = settings.arguments as Map;
            return MaterialPageRoute(
                builder: (context) => RoomList(
                      point: temp['point'],
                      building: temp['building'],
                    ));
          }

          //添加房屋
          if (settings.name == '/house_form') {
            return MaterialPageRoute(
                builder: (context) => HouseForm(
                      houseInfo: settings.arguments as Map,
                    ));
          }

          //房屋详情
          if (settings.name == '/house_detail') {
            return MaterialPageRoute(
                builder: (context) => HouseDetail(
                      houseInfo: settings.arguments as Map,
                    ));
          }

          //没有匹配到  //先去首页
          return MaterialPageRoute(builder: (context) => TabBarPage());
        },
        initialRoute: '/'
        //
        ),
  );
}
